<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;

class Index extends Common
{
	//框架
    public function index()
    {
        //绘制菜单
       // print_r(Session::get());die;
        $uid=Session::get("login_id");
        if($uid==1){
            $where="1=1";
        }else{
            $role=db("users_role ur")->join("role r","r.id=ur.role_id")->where("ur.users_id=$uid and r.is_deleted=0 and r.is_actived=1")->value("role_id");
            //角色组处理
            $roleg=db("users_role_group ug")->join("role_group rg","ug.role_group_id=rg.id")->where("ug.users_id=$uid and rg.is_deleted=0 and rg.is_actived=1")->value("role_group_id");
            if($roleg){
                $rg=db("role_edit")->where("group_id=$roleg")->select();
            }else{
                $rg=[];
            }
            $rgp=[];
            for($i=0;$i<count($rg);$i++){
                $tmppower=db("role")->where("id={$rg[$i]["role_id"]}")->value("power");
                if($tmppower){
                    $tmppower=explode(',', $tmppower);
                    $rgp=array_unique(array_merge($rgp,$tmppower));
                }
                

            }
            //echo count($rgp);
            //print_r($rgp);
            if($role)
                $power=db("role")->where("id=$role")->value("power");
            else
                $power="";
            if($power){
                $power=explode(',',$power);
               // echo "|".count($power)."|";
                $power=array_unique(array_merge($power,$rgp));
               // echo "|".count($power)."|";
                $rolelists=implode(',', $power);
               //print_r($power);
                $where="id in ($rolelists)";
            }else{
                if($rgp){
                    $rolelists=implode(',',$rgp);
                    $where="id in ($rolelists)";
                }else{
                    $where="1<>1";
                }
                
            }
            
            
        }
        //echo $where;
        $rolelist=db("power")->where($where." and level=1")->order("`index`")->select();
       // echo db("power")->getLastSql();exit;
        //print_r($rolelist);
        for($i=0;$i<count($rolelist);$i++){
            $rolelist[$i]["child"]=db("power")->where($where." and level=2 and pid={$rolelist[$i]["id"]}")->order("`index`")->select();
            for($j=0;$j<count($rolelist[$i]["child"]);$j++){
                $rolelist[$i]["child"][$j]["child"]=db("power")->where($where." and level=3 and pid={$rolelist[$i]["child"][$j]["id"]}")->order("`index`")->select();
            }
        }
        $str="";
        for($i=0;$i<count($rolelist);$i++){
            $str.="<li>";
            $str.="<a href='#'>&nbsp;<i class='{$rolelist[$i]['img']}' style='font-size: 22px'></i><span class='nav-label'>{$rolelist[$i]['title']}</span></a>";
            $str.="<ul class='nav nav-second-level'>";
            for($j=0;$j<count($rolelist[$i]["child"]);$j++){
                $str.="<li>";
                $str.="<a class='J_menuItem'><span class='fa arrow'></span><span class='nav-label'>{$rolelist[$i]["child"][$j]["title"]}</span></a>";
                $str.="<ul class='nav nav-third-level collapse'>";
                    for($k=0;$k<count($rolelist[$i]["child"][$j]["child"]);$k++){
                        if($rolelist[$i]["child"][$j]["child"][$k]["canshu"])
                            $str.="<li><a class='J_menuItem' href=".url($rolelist[$i]["child"][$j]["child"][$k]["path"])."?".$rolelist[$i]["child"][$j]["child"][$k]["canshu"].">{$rolelist[$i]["child"][$j]["child"][$k]["title"]}</a></li>";
                        else
                            $str.="<li><a class='J_menuItem' href=".url($rolelist[$i]["child"][$j]["child"][$k]["path"]).">{$rolelist[$i]["child"][$j]["child"][$k]["title"]}</a></li>";
                    }
                $str.="</ul>";
                $str.="</li>";  
            }
            $str.="</ul>";
            $str.="</li>";
        }
        $this->assign("str",$str);
        //$this->assign("rolelist",$rolelist);
        //echo count($rolelist);
        return $this->fetch('index');
    }
    //主页欢迎页面
    public function hello(){
        $post_data = [];
        $post_data['accessToken'] = session('accessToken');
        $post_data['requestType'] = 'TodoList';
        //dump($post_data);die;
        $page = $this->request->param('page')?$this->request->param('page'):1;
        $data1 = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/todo/page.wf',$post_data);
        if(!isset($data1->data)||!is_array($data1->data)){
            echo '';
        }
        $dbzj = count($data1->data);
        $this->assign('dbzj',$dbzj);
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/todo/page.wf?page='.$page.'&limit=8',$post_data);
       //dump($data);die;
        if(!$data){
            echo "无工作流";die;
        }
        if(!isset($data->data)||!is_array($data->data)){
            echo '';
        }
        foreach($data->data as $k=>$v){
            $workId = $v->workId;
            $v->myId = db('cg_workflow')->where('workId',$workId)->value('id');
            $v->ywlx = db('cg_workflow')->where(['workId'=>$v->workId])->value('ywlx');
            $v->rwmcjbh = db('cg_workflow')->where(['workId'=>$v->workId])->value('rwmcjbh');
            $v->status = db('cg_workflow')->where(['workId'=>$v->workId])->value('status');
            $v->arrive_time = db('cg_workflow')->where(['workId'=>$v->workId])->value('arrive_time');
            $v->department_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('department_id');
            $v->sqr_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('sqr_id');
            $v->type = db('cg_workflow_ywlx')->where('id',$v->ywlx)->value('title');
            $v->department = db('department')->where('id',$v->department_id)->value('de_name');
            $v->sqr = db('employee')->where('id',$v->sqr_id)->value('em_name');
            $data->data[$k] = $v;
        }
        $data->data = object_array($data->data);
        $this->assign('data',$data->data);

        //已办
        $post_data['accessToken'] = session('accessToken');
        $post_data['requestType'] = 'HadDone';
        $page = $this->request->param('page')?$this->request->param('page'):1;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/haddone/page.wf?page='.$page.'&limit=8',$post_data);
//        dump($data);
        foreach($data->data as $k=>$v){
            $workId = $v->workId;
            $v->myId = db('cg_workflow')->where('workId',$workId)->value('id');
            $v->ywlx = db('cg_workflow')->where(['workId'=>$v->workId])->value('ywlx');
            $v->rwmcjbh = db('cg_workflow')->where(['workId'=>$v->workId])->value('rwmcjbh');
            $v->status = db('cg_workflow')->where(['workId'=>$v->workId])->value('status');
            $v->arrive_time = db('cg_workflow')->where(['workId'=>$v->workId])->value('arrive_time');
            $v->department_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('department_id');
            $v->sqr_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('sqr_id');
            $v->type = db('cg_workflow_ywlx')->where('id',$v->ywlx)->value('title');
            $v->department = db('department')->where('id',$v->department_id)->value('de_name');
            $v->sqr = db('employee')->where('id',$v->sqr_id)->value('em_name');
            $data->data[$k] = $v;
        }

        $data->data = object_array($data->data);
        $this->assign('ybdata',$data->data);

        //待阅
        $post_data = [];
        $post_data['accessToken'] = session('accessToken');
        $post_data['requestType'] = 'MatterRead';
        $page = $this->request->param('page')?$this->request->param('page'):1;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/todo/page.wf?page='.$page.'&limit=10',$post_data);
//        dump($data);
        if(!$data){
            echo "无工作流";die;
        }
        if(!isset($data->data)||!is_array($data->data)){
            echo '';
        }
        foreach($data->data as $k=>$v){
            $workId = $v->workId;
            $v->myId = db('cg_workflow')->where('workId',$workId)->value('id');
            $v->ywlx = db('cg_workflow')->where(['workId'=>$v->workId])->value('ywlx');
            $v->rwmcjbh = db('cg_workflow')->where(['workId'=>$v->workId])->value('rwmcjbh');
            $v->status = db('cg_workflow')->where(['workId'=>$v->workId])->value('status');
            $v->arrive_time = db('cg_workflow')->where(['workId'=>$v->workId])->value('arrive_time');
            $v->department_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('department_id');
            $v->sqr_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('sqr_id');
            $v->type = db('cg_workflow_ywlx')->where('id',$v->ywlx)->value('title');
            $v->department = db('department')->where('id',$v->department_id)->value('de_name');
            $v->sqr = db('employee')->where('id',$v->sqr_id)->value('em_name');
            $data->data[$k] = $v;
        }
        $data->data = object_array($data->data);
        $this->assign('dydata',$data->data);

        //已阅
        $post_data['accessToken'] = session('accessToken');
        $post_data['requestType'] = 'HadRead';
        $page = $this->request->param('page')?$this->request->param('page'):1;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/haddone/page.wf?page='.$page.'&limit=8',$post_data);
//        dump($data);
        foreach($data->data as $k=>$v){
            $workId = $v->workId;
            $v->myId = db('cg_workflow')->where('workId',$workId)->value('id');
            $v->ywlx = db('cg_workflow')->where(['workId'=>$v->workId])->value('ywlx');
            $v->rwmcjbh = db('cg_workflow')->where(['workId'=>$v->workId])->value('rwmcjbh');
            $v->status = db('cg_workflow')->where(['workId'=>$v->workId])->value('status');
            $v->arrive_time = db('cg_workflow')->where(['workId'=>$v->workId])->value('arrive_time');
            $v->department_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('department_id');
            $v->sqr_id = db('cg_workflow')->where(['workId'=>$v->workId])->value('sqr_id');
            $v->type = db('cg_workflow_ywlx')->where('id',$v->ywlx)->value('title');
            $v->department = db('department')->where('id',$v->department_id)->value('de_name');
            $v->sqr = db('employee')->where('id',$v->sqr_id)->value('em_name');
            $data->data[$k] = $v;
        }

        $data->data = object_array($data->data);
        $this->assign('yydata',$data->data);

        //通知公告
        $tzgg = db('cg_ggxx')->order('fbrq desc')->limit('7')->select();
        $this->assign('tzgg',$tzgg);

        //提醒信息
        $msg = db('cg_message')->where('txry','like','%"'.session('employee_id').'"%')->order('fssj desc')->limit('8')->select();
        $this->assign('msg',$msg);
		return $this->fetch('hello');
    }
}
